package com.jzz.eletuanbackend.service;

import com.alibaba.fastjson.JSONObject;
import com.jzz.eletuanbackend.mapper.DelivererMapper;
import com.jzz.eletuanbackend.mapper.OrderMapper;
import com.jzz.eletuanbackend.util.Msg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class DelivererService {

    public static final String prefix = "ELT-DLVRPOS-";

    @Autowired
    private DelivererMapper delivererMapper;

    @Autowired
    private RedisService redisService;

    @Autowired
    private OrderMapper orderMapper;

    public void updatePosition(Long userId, Double longitude, Double latitude) {
        if (!redisService.exists(prefix + userId)) {
            redisService.add(prefix + userId, longitude + " " + latitude);
        } else {
            redisService.set(prefix + userId, longitude + " " + latitude);
        }
    }

    public Msg getPosition(Long userId) {
        double longitude = 0.0;
        double latitude = 0.0;
        if (redisService.exists(prefix + userId)) {
            String s = (String) redisService.get(prefix + userId);
            longitude = Double.parseDouble(s.split(" ")[0]);
            latitude = Double.parseDouble(s.split(" ")[1]);
        }

        JSONObject object = new JSONObject();
        object.put("longitude", longitude);
        object.put("latitude", latitude);
        return Msg.ok("success", object);
    }

    @Transactional
    public Msg takeOrder(Long userId, Long orderId) {
        if (orderMapper.isOrderCanceled(orderId) == 0) {
            if (orderMapper.takeOrder(orderId, System.currentTimeMillis(), userId) > 0) {
                return Msg.ok("接单成功，请尽快赶往商家");
            }
        } else {
            return Msg.err(-1, "订单已被取消");
        }

        return Msg.err(-1, "接单失败");
    }

    @Transactional
    public Msg arriveOrder(Long orderId) {
        if (orderMapper.isOrderCanceled(orderId) == 0) {
            if (orderMapper.arriveOrder(orderId, System.currentTimeMillis()) > 0) {
                return Msg.ok("到达商家，请等待商家出餐");
            }
        } else {
            return Msg.err(-1, "订单已被取消");
        }

        return Msg.err(-1, "操作失败");
    }

    @Transactional
    public Msg deliverOrder(Long orderId) {
        if (orderMapper.isOrderCanceled(orderId) == 0) {
            if (orderMapper.deliverOrder(orderId, System.currentTimeMillis()) > 0) {
                return Msg.ok("已送出餐品");
            }
        } else {
            return Msg.err(-1, "订单已被取消");
        }

        return Msg.err(-1, "操作失败");
    }

    @Transactional
    public Msg receiveOrder(Long orderId) {
        if (orderMapper.receiveOrder(orderId, System.currentTimeMillis()) > 0) {
            return Msg.ok("已送达餐品");
        }
        return Msg.err(-1, "操作失败");
    }

    public Msg getAllOrders(Long userId) {
        return Msg.ok("success", orderMapper.getByDelivererId(userId));
    }

    public Msg getOrder(Long id) {
        return Msg.ok("success", orderMapper.getById(id));
    }

    public Msg availableOrders() {
        return Msg.ok(orderMapper.availableOrders());
    }
}
